﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using PlanningGIS.Framework;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
//using ESRI.ArcGIS.TOCControl;
using System.Windows.Forms;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：历史数据归档
    /// 作者：
    /// 编写日期：2018-03-25
    /// </summary>
    internal  class HistoryArchive:BaseCommand 
    {
        IApplication m_Application = null;
        public HistoryArchive()
        {
            base.m_caption = "历史归档数据";
            base.m_toolTip = base.Caption;
        }

        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        public override void OnClick()
        {
            IFeatureLayer pLayer = (m_Application as IFramework).TOCControl.CustomProperty as IFeatureLayer;
            IFeatureClass pFeatClsArchive = null;
            IFeatureClass pFeatCls = null;
            IDataset pDs = null;
            try
            {
                pFeatCls = pLayer.FeatureClass;
                pDs = pFeatCls as IDataset;
                pFeatClsArchive =
                    ((IFeatureWorkspace)pDs.Workspace).
                        OpenFeatureClass(pDs.Name + "_H");
            }
            catch
            {
                MsgHelper.ShowInfomationMsgbox("该图层无历史归档数据！");
                return;
            }
            if (pFeatClsArchive == null) return;
            IFeatureLayer pFeatLyrArchive =
                new FeatureLayerClass();
            pFeatLyrArchive.FeatureClass = pFeatClsArchive;
            pFeatLyrArchive.Name = pDs.Name + "归档";
            (m_Application as IFramework).MapControl.AddLayer(pFeatLyrArchive,0);
            (m_Application as IFramework).MapControl.Refresh(esriViewDrawPhase.esriViewGeography,null,null);
        }
        public override bool Enabled
        {
            get
            {
                try
                {
                    if ((m_Application as IFramework).IsMapCtrlactive && ((m_Application as IFramework).TOCControl.CustomProperty is IFeatureLayer))
                    {
                        IFeatureLayer pLayer = (m_Application as IFramework).TOCControl.CustomProperty as IFeatureLayer;
                        IFeatureClass pFeatCls = pLayer.FeatureClass;
                        if ((pFeatCls as IDataset).Workspace.Type != esriWorkspaceType.esriRemoteDatabaseWorkspace) return false;
                        IVersionedObject3 versionedObject = (IVersionedObject3)pFeatCls;
                        IArchivableObject archivableObject = (IArchivableObject)versionedObject;
                        return archivableObject.IsArchiving;
                    }
                    return false;
                }
                catch
                {
                    return false;
                }
            }
        }
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
    
}
